package com.itlxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itlxd.entity.Employee;
import com.itlxd.enums.EYesOrNo;
import com.itlxd.global.SysConstants;
import com.itlxd.mapper.EmployeeMapper;
import com.itlxd.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper mapper;

    @Override
    public Employee getOne(Employee e) {
        String username = e.getUsername();
        String password = DigestUtils.md5DigestAsHex(e.getPassword().getBytes());

        LambdaQueryWrapper<Employee> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(Employee::getUsername, username)
                .eq(Employee::getPassword, password);

        return mapper.selectOne(lambdaQuery);
    }

    @Override
    public Boolean usernameIsExist(String username) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, username);
        Employee employee = mapper.selectOne(queryWrapper);
        if (null != employee) {
            return true;
        }
        return false;
    }

    @Override
    public Integer saveEmployee(Employee employee) {
        return mapper.insert(employee);
    }

    @Override
    public IPage<Employee> listPageEmployees(Integer currentPage, Integer pageSize, String name) {
        IPage<Employee> iPage = new Page(currentPage, pageSize);
        LambdaQueryWrapper<Employee> lambdaQuery = new LambdaQueryWrapper();
        lambdaQuery.like(null != name, Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime);
        return mapper.selectPage(iPage, lambdaQuery);
    }

    @Override
    public Employee getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public Integer updateEmployee(Employee employee) {
        employee.setUpdateUser(999999999999999L);
        return mapper.updateById(employee);
    }
}
